﻿namespace Arms.Framework.Collections
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;

    /// <summary>
    /// Add only dictionary.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class AddOnlyDictionary<TKey, TValue> : IAddOnlyDictionary<TKey, TValue>
    {
        private Dictionary<TKey, TValue> _items;

        public AddOnlyDictionary() : this(new Dictionary<TKey, TValue>()) { }
        public AddOnlyDictionary(Dictionary<TKey, TValue> values)
        {
            _items = values;
        }
        /// <summary>
        /// Gets the value at this index.
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public TValue this[TKey key]
        {
            get { return _items[key]; }
        }
        /// <summary>
        /// Number of items in this collection.
        /// </summary>
        public int Count { get { return _items.Count; } }
        /// <summary>
        /// Adds an item to this dictionary.
        /// </summary>
        /// <param name="key">key</param>
        /// <param name="value">value</param>
        public void Add(TKey key, TValue value)
        {
            _items.Add(key, value);
        }
        /// <summary>
        /// Adds an item to this dictionary.
        /// </summary>
        /// <param name="key">key</param>
        /// <param name="value">value</param>
        public void Add(KeyValuePair<TKey, TValue> item)
        {
            _items.Add(item.Key, item.Value);
        }
        /// <summary>
        /// Adds a distinct item to this dictionary.
        /// </summary>
        /// <param name="key">key</param>
        /// <param name="value">value</param>
        public void AddDistinct(KeyValuePair<TKey, TValue> item)
        {
            this.Add(item);
        }

        public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
        {
            return _items.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }

        public static explicit operator AddOnlyDictionary<TKey, TValue>(Dictionary<TKey, TValue> values)
        {
            return new AddOnlyDictionary<TKey, TValue>(values);
        }

        public static implicit operator Dictionary<TKey, TValue>(AddOnlyDictionary<TKey, TValue> values)
        {
            return values.ToDictionary(x => x.Key, x => x.Value);
        }
    }
}
